#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
1.导包
2.获取测试数据
3.定义一个测试类
    3.1创建测试用例方法
    3.2获取测试数据内进行请求所需要的关键字段
    3.3进行requests请求，获得返回结果
    3.4断言接口返回结果，判断成功失败
'''
# 1.导包
import requests
import unittest
from interface01.common.readExcle import *
from interface01.common.configHttp import *
from interface01.common.writeExcle import *
from ddt import ddt,data,unpack



# 2.获取测试数据
data_obj = ReadExcle()
#调用实例方法
test_data = data_obj.read()
# 3.定义一个测试类
@ddt
class TestCase(unittest.TestCase):
#     3.1创建测试用例方法
    @data(*test_data)
    @unpack
    def test_case(self, id, interfaceUrl, name, method, value, expect, real, status):
#       3.2获取测试数据内进行请求所需要的关键字段
#         url = test_data[0]['interfaceUrl']
#         method = test_data[0]['method']
#         value = test_data[0]['value']
#         expect = int(test_data[0]['expect'])
#       3.3进行requests请求，获得返回结果
        value = eval(value)
        expect = int(expect)
        # if method == 'get':
        #     res = requests.get(url=interfaceUrl, params=value)
        # elif method == 'post':
        #     res = requests.post(url=interfaceUrl, data=value)

        res = ConfigHttp(method, interfaceUrl, value)
        re = res.run()
        res_dict = re.json()

        #获取实际结果
        real = res_dict['errorCode']

        #对接口返回进行断言
        try:
            if real == expect:
                status = 'pass'
            else:
                status = 'fail'
            we = WriteExcle()
            we.write_data(int(id), 6, real, status)
        except Exception as msg:
            print(f'报错了{msg}')
            raise

#       3.4断言接口返回结果，判断成功失败
        self.assertEqual(res_dict['errorCode'], expect, '测试用例不通过')


if __name__ == '__main__':
    unittest.main(verbosity=2)
